﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReporterControlLibrary.Models;
using ReporterControlLibrary.UIHelpers;
using ReporterEngine;
using ReporterSystem;
using System.Windows.Interactivity;


namespace ReporterControlLibrary
{
    /// <summary>
    /// Interaction logic for EvaluatePath.xaml
    /// </summary>
    public partial class EvaluatePath : UserControl
    {
        private Project _project;

        public EvaluatePath()
        {
            InitializeComponent();
        }

        public Project Project
        {
            get
            {
                if (DataContext != null && DataContext is MetaModel)
                {
                    _project = (DataContext as MetaModel).Project;
                    return _project;
                }
                return null;
            }
        }

        private void EvaluatePathBtClick(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(PathBox.Text)) return;

            Project.ReportProject.EvalPath = PathBox.Text;
            ReportApp.UpdateProjectInfo(Project.ReportProject);

            david.Eval(Project);

        }

        //public void Evaluate()
        //{
        //    var model = ReportApp.GetModel(this.Project.ReportProject);

        //    sQuery query = sQuery.New(model);
        //    query.Elements.Add(model.Notebook);
            

        //    var actualPath = LexPath.Scan(this.Project.MetaModel.EvalExpression);
        //    Interpreter target = Interpreter.Create(actualPath);

        //    var actual = target.Execute(actualPath, query);
        //    //show eerors and log

        //    var tres = model.GetInstanceCategories(actual);

        //    this.Project.MetaModel.EvalResult = tres;
        //    //}
        //    var am = new AvailableModel();
        //   // am.ModelProperties = new ObservableCollection<ModelProperty> { new ModelProperty { Name = "popery 1" }, new ModelProperty { Name = "popery 2" }, new ModelProperty { Name = "popery 3" } };
        //   // am.ModelReferences = new ObservableCollection<ModelReference> { new ModelReference { Name = "reference 1" }, new ModelReference { Name = "reference 2" }, new ModelReference { Name = "reference 3" } };
        //   // am.ModelUsedBys = new ObservableCollection<ModelUsedBy> { new ModelUsedBy { Name = "object 1" }, new ModelUsedBy { Name = "object 2" }, new ModelUsedBy { Name = "object 3" } };
        //    am.ModelMethods = new ObservableCollection<ModelMethod> { new ModelMethod { Name = "function 1", Description = "This si sample decription of method functionalities 1  ..." }, new ModelMethod { Name = "function 2", Description = "This si sample decription of method functionalities 2 ..." }, new ModelMethod { Name = "function 3", Description = "This si sample decription of method functionalities 3 ..." } };
        //    //am.ModelTypes = new ObservableCollection<ModelType> { new ModelType { Name = "type 1" }, new ModelType { Name = "type 2" }, new ModelType { Name = "type 3" } };

        //    am.ModelTypes = new ObservableCollection<ModelType>();
        //    foreach (var modelType in actual._model.GetModelTypes(actual).Take(10))
        //    {
        //        am.ModelTypes.Add(modelType);
        //    }

        //    am.ModelProperties = new ObservableCollection<ModelProperty> { new ModelProperty { Name = "id" }, new ModelProperty { Name = "name" }, new ModelProperty { Name = "type" } };
        //    foreach (var modelType in actual._model.GetModelproperties(actual).Take(10))
        //    {
        //        am.ModelProperties.Add(modelType);
        //    }

        //    am.ModelReferences = new ObservableCollection<ModelReference>();
        //    foreach (var modelType in actual._model.GetModelreferencies(actual).Take(10))
        //    {
        //        am.ModelReferences.Add(modelType);
        //    }

        //    am.ModelUsedBys = new ObservableCollection<ModelUsedBy>();
        //    foreach (var modelType in actual._model.GetModelUsedBy(actual).Take(10))
        //    {
        //        am.ModelUsedBys.Add(modelType);
        //    }

        //    Project.MetaModel.ModelStructure = am;
        //}

        private static void UpdateSource(object sender)
        {
            var tb = sender as TextBox;

            if (tb == null) return;

            BindingExpression exp = tb.GetBindingExpression(TextBox.TextProperty);
            if (exp != null) exp.UpdateSource();
        }

        private void PathBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateSource(sender);
        }

        private void PathBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && !String.IsNullOrEmpty(PathBox.Text))
            {
                Project.ReportProject.EvalPath = PathBox.Text;
                ReportApp.UpdateProjectInfo(Project.ReportProject);

                david.Eval(Project);
            }
        }

        private void EvaluatePathToolbox_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
